package com.neutech.web;

import com.neutech.dao.UserDAO;
import com.neutech.dao.impl.UserDAOImpl;
import com.neutech.entity.User;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "userServlet",urlPatterns = {"*.user"})
public class UserServlet extends HelloServlet {
    private UserDAO dao = new UserDAOImpl();
    @Override
    public void init() throws ServletException {
        System.out.println("初始化");
    }

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = request.getRequestURL().toString();
        String pathUri =request.getRequestURI();
        System.out.println("对象已经实例化");
        path = path.substring(path.lastIndexOf("/"),path.lastIndexOf("."));
        /**
         * 后端到前端的编码集
         */
        response.setContentType("text/html;charset=utf-8");
        /**
         * 前端到后盾的编码集
         */
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        if ("/findAll".equals(path)){
            System.out.println("123333");
            //查询所有
            List<User> users = dao.selectAll();
            /**
             * 转发到jsp页面进行处理，转发是带有数据的
             * 重定向是没有数据相当于跳转的
             */
            /**
             * 1、下面这一步是将数据绑定到request上
             */
            request.setAttribute("users",users);
            /**
             * 2、获取转发器，这就是告诉我们，要交给那个页面处理绑定的数据
             * 返回一个RequestDispatcher类型的转发器
             */
             RequestDispatcher rd = request.getRequestDispatcher("user_list.jsp");
            /**
             * 3、转发，把所有都转发
             */
            rd.forward(request,response);
        }else if ("/insert".equals(path)){
            //把从前端的数据拿过来传到服务器
            String name = request.getParameter("name");
            String password = request.getParameter("password");
            String phone = request.getParameter("phone");
            String address = request.getParameter("address");
            dao.insert(new User(name,password,phone,address));
            //做完之后在跳转到列表页
            //重定向
            response.sendRedirect("findAll.user");
        }else if ("/delete".equals(path)){
            //删除
            //从前端获取到id
            int id = Integer.parseInt(request.getParameter("id"));
            //根据id进行删除
            dao.delete(id);
            //重定向到列表页面
            response.sendRedirect("findAll.user");
        }else if ("/load".equals(path)){
            //修改是两步完成的
            //修改肯定是根据某个id删除某个人的数据，先拿到某个人的id
            int id = Integer.parseInt(request.getParameter("id"));
            User user = dao.selectById(id);
            //绑定到request上，然后转发
            request.setAttribute("user",user);
            //然后转发到修改页面
            RequestDispatcher rd = request.getRequestDispatcher("user_update.jsp");
            //实现转发
            rd.forward(request,response);

        }else if ("/update".equals(path)){
            //修改是两步完成的
            int id = Integer.parseInt(request.getParameter("id"));
            User user = dao.selectById(id);
            //从前端把name拿过来，就和上面的id是一样的
            String name = request.getParameter("name");
            String password = request.getParameter("password");
            String phone = request.getParameter("phone");
            String address = request.getParameter("address");
            user.setName(name);
            user.setPassword(password);
            user.setPhone(phone);
            user.setAddress(address);
            dao.update(user);
            response.sendRedirect("findAll.user");
        }
        else {
            //重定向
            System.out.println("路径错误");
            response.sendRedirect("404.jsp");
        }
    }
    public void findAll(HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset = utf-8");

            PrintWriter out = response.getWriter();
        UserDAO dao = new UserDAOImpl();
        List<User> users = dao.selectAll();
        for (User user:users){
            out.println("<p>"+ user+"</p>");
        }


    }
}

















